<script setup lang="ts">
  import { NButton, NSpin, useMessage } from 'naive-ui'
  import { onMounted, ref } from 'vue'
  import { useRoute, useRouter } from 'vue-router'
  import BackNav from '@/components/common/BackNav/index.vue'
  import PassModal from './PassModal.vue'
  import { auditScheme, getSchemeCategoryDetail } from '@/api/scheme'
  import WordPreview from '@/components/WordPreview/index.vue'
  import { useAgent } from '@/hooks/useAgent'
  const { userName } = useAgent()
  const route = useRoute()
  const id = route.params.id as string
  console.log('方案id', id)
  const passModalRef = ref()
  const wordPreviewId = ref(`${new Date().getTime()}`)
  const handleAudit = (pass: boolean) => {
    passModalRef.value.handleOpen(pass)
  }

  const loading = ref(false)
  onMounted(async () => {
    await getDetail()
  })

  const detail = ref<any>({})
  const getDetail = async () => {
    loading.value = true
    const res = await getSchemeCategoryDetail({ id })
    detail.value = res
    loading.value = false
  }

  const router = useRouter()
  const message = useMessage()
  const handleSubmit = async (data: any) => {
    // 先未审核状态
    await auditScheme({
      schemeAuditId: id,
      auditStatus: 0,
      type: 1,
    })

    await auditScheme({
      schemeAuditId: id,
      auditStatus: data.pass ? 1 : 2,
      type: 1,
      auditResult: data.auditResult,
    })

    message.success('审核成功')
    router.back()
  }
</script>

<template>
  <div class="h-full flex flex-col py-5 gap-4">
    <BackNav title="原文概览">
      <template #right>
        <div class="flex items-center gap-4">
          <n-button class="px-4 h-8" type="primary" @click="handleAudit(true)">通过</n-button>
          <div
            class="px-4 h-8 leading-8 border border-[#0B5CFF] text-[#0B5CFF] rounded cursor-pointer hover:border-[#0B5CFFCC] hover:text-[#0B5CFFCC]"
            @click="handleAudit(false)"
          >
            不通过
          </div>
        </div>
      </template>
    </BackNav>
    <div class="h-[calc(100%-74px)]">
      <div class="h-full bg-white rounded p-5">
        <n-spin :show="loading" class="w-full h-full">
          <WordPreview
            v-if="detail.schemeUrl"
            :file="detail.schemeUrl"
            :id="wordPreviewId"
            mode="view"
            :userName="userName"
          />
        </n-spin>
      </div>
    </div>
    <PassModal ref="passModalRef" @submit="handleSubmit" />
  </div>
</template>

<style scoped lang="less">
  :deep(.n-spin-content) {
    width: 100%;
    height: 100%;
  }
</style>
